| 1 | import { readFile } from "node:fs/promises"; |
| 2 | import path from "node:path"; |
| 3 | import { UPLOADS_DIR, UPLOAD_NAME_RE, contentTypeFor } from "@/lib/uploads"; |
| 4 | |
| 5 | export async function GET( |
| 6 | _request: Request, |
| 7 | { params }: { params: Promise<{ name: string }> }, |
| 8 | ) { |
| 9 | const { name } = await params; |
| 10 | if (!UPLOAD_NAME_RE.test(name)) return new Response("Not found", { status: 404 }); |
| 11 | |
| 12 | const type = contentTypeFor(name.split(".")[1] ?? ""); |
| 13 | if (!type) return new Response("Not found", { status: 404 }); |
| 14 | |
| 15 | try { |
| 16 | const buf = await readFile(path.join(UPLOADS_DIR, name)); |
| 17 | return new Response(new Uint8Array(buf), { |
| 18 | headers: { |
| 19 | "Content-Type": type, |
| 20 | "Content-Length": String(buf.length), |
| 21 | "Cache-Control": "public, max-age=31536000, immutable", |
| 22 | "X-Content-Type-Options": "nosniff", |
| 23 | "Content-Disposition": "inline", |
| 24 | }, |
| 25 | }); |
| 26 | } catch { |
| 27 | return new Response("Not found", { status: 404 }); |
| 28 | } |
| 29 | } |